package com.guidance.guidance.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.guidance.guidance.domain.Message;
import com.guidance.guidance.domain.User;
import com.guidance.guidance.repository.UserRepository;
import com.guidance.guidance.utils.CommonsUtils;

@RestController
@RequestMapping("/register")
public class RegisterController {

	@Autowired
	private UserRepository userRepository;

	/**
	 * 添加用户信息
	 * 客户端可通过此API实现注册功能,所需传入参数有name,telephone,password
	 * @param user
	 * @return
	 */
	@RequestMapping("/userRegister")
	public Message<User> userAdd(User user, Message<User> msg) {
		if (userRepository.findByTelephone(user.getTelephone()) != null) {
			msg.setCode("404");
			msg.setMsg("该用户已存在");
			msg.setSuccess("false");
			return msg;
		}
		user.setUid(CommonsUtils.getUUID());
//		user.setName(user.getName());
//		user.setTelephone(user.getTelephone());
//		user.setPassword(user.getPassword());
		user.setFlg(3);
		User u = userRepository.save(user);
		if (u != null) {
			msg.setCode("200");
			msg.setMsg("注册成功");
			msg.setSuccess("true");
		} else {
			msg.setCode("404");
			msg.setMsg("注册失败");
			msg.setSuccess("false");
		}
		return msg;
	}

	// @RequestMapping("/userRegister")
	// public User register(String name , String telephone , String password) {
	// User user = new User();
	// user.setUid(CommonsUtils.getUUID());
	// user.setName(name);
	// user.setTelephone(telephone);
	// user.setPassword(password);
	// user.setFlg(3);
	// return user;
	// }

}
